<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="客户ID" prop="customerId">
        <el-input v-model="queryParams.customerId" placeholder="请输入客户ID" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="客户姓名" prop="customerName">
        <el-input v-model="queryParams.customerName" placeholder="请输入客户姓名" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="发票样式" prop="style">
        <el-input v-model="queryParams.style" placeholder="请输入发票样式" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="queryParams.email" placeholder="请输入邮箱" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="是否默认0：不是，1：默认" prop="defaultFlag">
        <el-input v-model="queryParams.defaultFlag" placeholder="请输入是否默认0：不是，1：默认" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="注册手机" prop="rejectTel">
        <el-input v-model="queryParams.rejectTel" placeholder="请输入注册手机" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="开户银行" prop="companyBankName">
        <el-input v-model="queryParams.companyBankName" placeholder="请输入开户银行" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="银行账号" prop="companyBankAccount">
        <el-input v-model="queryParams.companyBankAccount" placeholder="请输入银行账号" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="地区" prop="companyArea">
        <el-input v-model="queryParams.companyArea" placeholder="请输入地区" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:head:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:head:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:head:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:head:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="headList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="记录ID" align="center" prop="id" />
      <el-table-column label="客户ID" align="center" prop="customerId" />
      <el-table-column label="客户姓名" align="center" prop="customerName" />
      <el-table-column label="发票类型" align="center" prop="invcoieType" />
      <el-table-column label="发票样式" align="center" prop="style" />
      <el-table-column label="抬头类型" align="center" prop="headType" />
      <el-table-column label="抬头" align="center" prop="headInfo" />
      <el-table-column label="邮箱" align="center" prop="email" />
      <el-table-column label="省市区" align="center" prop="area" />
      <el-table-column label="详细地址" align="center" prop="address" />
      <el-table-column label="是否默认" align="center" prop="defaultFlag" />
      <el-table-column label="税 号" align="center" prop="taxNo" />
      <el-table-column label="注册地址" align="center" prop="rejectAddress" />
      <el-table-column label="注册手机" align="center" prop="rejectTel" />
      <el-table-column label="开户银行" align="center" prop="companyBankName" />
      <el-table-column label="银行账号" align="center" prop="companyBankAccount" />
      <el-table-column label="地区" align="center" prop="companyArea" />
      <el-table-column label="详细地址" align="center" prop="companyAddress" />
      <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:head:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:head:remove']">删除</el-button>
        </template>
</el-table-column> -->
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改发票抬头对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="客户姓名">
          <el-select v-model="form.customerId" placeholder="请选择客户" filterable @change="handleSelectChange">
            <el-option v-for="item in customerList" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>


        <!-- <el-form-item label="客户ID" prop="customerId">
          
          <el-input v-model="form.customerId" placeholder="请输入客户ID" />
        </el-form-item> -->
        <!-- <el-form-item label="客户姓名" prop="customerName">
          <el-input v-model="form.customerName" placeholder="请输入客户姓名" readonly />
        </el-form-item> -->
        <el-form-item label="发票样式" prop="style">
          <el-input v-model="form.style" placeholder="请输入发票样式" />
        </el-form-item>
        <el-form-item label="抬头" prop="headInfo">
          <el-input v-model="form.headInfo" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱" />
        </el-form-item>
        <el-form-item label="省市区" prop="area">
          <el-input v-model="form.area" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="form.address" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="是否默认" prop="defaultFlag">
          <el-input v-model="form.defaultFlag" placeholder="请输入是否默认0：不是，1：默认" />
        </el-form-item>
        <el-form-item label="注册地址" prop="rejectAddress">
          <el-input v-model="form.rejectAddress" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="注册手机" prop="rejectTel">
          <el-input v-model="form.rejectTel" placeholder="请输入注册手机" />
        </el-form-item>
        <el-form-item label="开户银行" prop="companyBankName">
          <el-input v-model="form.companyBankName" placeholder="请输入开户银行" />
        </el-form-item>
        <el-form-item label="银行账号" prop="companyBankAccount">
          <el-input v-model="form.companyBankAccount" placeholder="请输入银行账号" />
        </el-form-item>
        <el-form-item label="地区" prop="companyArea">
          <el-input v-model="form.companyArea" placeholder="请输入地区" />
        </el-form-item>
        <el-form-item label="详细地址" prop="companyAddress">
          <el-input v-model="form.companyAddress" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listHead, getHead, delHead, addHead, updateHead } from "@/api/system/head";
import { listCustomer, getCustomer } from "@/api/system/customer";

export default {
  name: "Head",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 发票抬头表格数据
      headList: [],
      //下拉框客户列表
      customerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        customerId: null,
        customerName: null,
        invcoieType: null,
        style: null,
        headType: null,
        headInfo: null,
        email: null,
        area: null,
        address: null,
        defaultFlag: null,
        taxNo: null,
        rejectAddress: null,
        rejectTel: null,
        companyBankName: null,
        companyBankAccount: null,
        companyArea: null,
        companyAddress: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询发票抬头列表 */
    getList() {
      this.loading = true;
      listHead(this.queryParams).then(response => {
        this.headList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        customerId: null,
        customerName: null,
        invcoieType: null,
        style: null,
        headType: null,
        headInfo: null,
        email: null,
        area: null,
        address: null,
        defaultFlag: null,
        taxNo: null,
        rejectAddress: null,
        rejectTel: null,
        companyBankName: null,
        companyBankAccount: null,
        companyArea: null,
        companyAddress: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加发票抬头";
      listCustomer().then(response => {
        this.customerList = response.rows;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getHead(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改发票抬头";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateHead(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addHead(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除发票抬头编号为"' + ids + '"的数据项？').then(function () {
        return delHead(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/head/export', {
        ...this.queryParams
      }, `head_${new Date().getTime()}.xlsx`)
    },
    //下拉框change事件
    handleSelectChange(value) {
      getCustomer(value).then(response => {
        console.log(response);
        this.form.customerName = response.data.name;
      });
    }
  }
};
</script>
